package io.renren.modules.frt.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.StringUtils;

import io.renren.common.utils.MapUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.frt.dao.TaskUserDao;
import io.renren.modules.frt.entity.TaskUserEntity;
import io.renren.modules.frt.service.TaskUserService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;


@Service("taskUserService")
public class TaskUserServiceImpl extends ServiceImpl<TaskUserDao, TaskUserEntity> implements TaskUserService {

	@Autowired
	private SysUserService sysUserService;
	
    @Override
    public PageUtils queryPage(Map<String, Object> params, TaskUserEntity taskUser) {
    	
    	Query<TaskUserEntity> query = new Query<TaskUserEntity>(params);
    	// 当前页，总条数 构造 page 对象
    	Page<TaskUserEntity> page = new Page<>(query.getCurrPage(), query.getLimit());
    	
    	String key = MapUtils.toStr(params, "key");
    	if(StringUtils.isNotEmpty(key)) {
    		taskUser.setTaskName("%"+key+"%");
    	}
    	
        page.setRecords(this.baseMapper.selectTaskPage(page, taskUser));
        
        return new PageUtils(page);
    }

	@Override
	public Long selectUnReportTaskByLevel(Long level, Long taskId, Long parentId) {
		return baseMapper.selectUnReportTaskByLevel(level, taskId, parentId);
	}

	@Override
	public PageUtils queryNextPage(Map<String, Object> params, TaskUserEntity taskUser) {

		Query<TaskUserEntity> query = new Query<TaskUserEntity>(params);
    	// 当前页，总条数 构造 page 对象
    	Page<TaskUserEntity> page = new Page<>(query.getCurrPage(), query.getLimit());
    	
        page.setRecords(this.baseMapper.selectNextPage(page, taskUser));
        
        return new PageUtils(page);
	}

	@Override
	@Transactional
	public void onlineCallbackTaskUser(Long taskUserId, Long reportId) {
		TaskUserEntity taskUser = this.selectById(taskUserId);

		taskUser.setReportId(reportId);

		this.updateById(taskUser);

		SysUserEntity noneUser = sysUserService.selectById(taskUser.getUserId());
		Long countryId = noneUser.getParentId();

		SysUserEntity countryUser = sysUserService.selectById(countryId);
		Long cityId = countryUser.getParentId();

		SysUserEntity cityUser = sysUserService.selectById(cityId);
		Long provinceId = cityUser.getParentId();

		List<TaskUserEntity> parentTU = this.selectList(new EntityWrapper<TaskUserEntity>()
				.eq("task_id", taskUser.getTaskId()).in("user_id", new Object[] { countryId, cityId, provinceId }));
		for (TaskUserEntity tu : parentTU) {
			tu.setReportId(reportId);
		}
		this.updateBatchById(parentTU);// 方便上级查看填报数据
	}

}
